package com.langChain4j.multiagent.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.base.frame.common.service.BaseServiceImpl;
import com.base.frame.exception.BusinessException;
import com.langChain4j.api.enums.CommonErrorEnum;
import com.langChain4j.multiagent.controller.vo.CreateTaskVo;
import com.langChain4j.multiagent.controller.vo.UpdateStatusVo;
import com.langChain4j.api.entity.MultiAgentCollaboration;
import com.langChain4j.multiagent.mapper.MultiAgentMapper;
import com.langChain4j.multiagent.service.MultiAgentService;
import java.time.LocalDateTime;
import org.springframework.stereotype.Service;

/**
 * 多智能体协作服务实现类
 *
 * @author linjun
 */
@Service
public class MultiAgentServiceImpl extends BaseServiceImpl<MultiAgentCollaboration, MultiAgentMapper> implements MultiAgentService {

    @Override
    public Long createTask(CreateTaskVo createTaskVo) {
        MultiAgentCollaboration entity = new MultiAgentCollaboration();
        entity.setTaskDescription(createTaskVo.getTaskDescription());
        entity.setAgentsInvolved(createTaskVo.getAgentsInvolved());
        entity.setStreamMode(createTaskVo.getStreamMode());
        entity.setStatus("pending");
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());

        save(entity);
        return entity.getCollaborationId();
    }

    @Override
    public MultiAgentCollaboration getTaskById(Long collaborationId) {
        MultiAgentCollaboration multiAgentCollaboration = getById(collaborationId);
        if (ObjectUtil.isNull(multiAgentCollaboration)) {
            throw new BusinessException(CommonErrorEnum.MULTI_AGENT_TASK_NOT_EXISTS);
        }
        return multiAgentCollaboration;
    }

    @Override
    public boolean updateStatus(UpdateStatusVo updateStatusVo) {
        MultiAgentCollaboration entity = getById(updateStatusVo.getCollaborationId());
        if (ObjectUtil.isNull(entity)) {
            throw new BusinessException(CommonErrorEnum.MULTI_AGENT_TASK_NOT_EXISTS);
        }
        entity.setStatus(updateStatusVo.getStatus());
        entity.setUpdateTime(LocalDateTime.now());
        updateById(entity);
        return true;
    }

}